//
//  JKBaseSettingTableViewController.swift
//  afanti
//
//  Created by albert on 16/5/7.
//  Copyright © 2016年 albert. All rights reserved.
//

import UIKit

private let ID = "settingCell"

class JKBaseSettingTableViewController: UITableViewController {
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    init() {
        super.init(style: UITableViewStyle.Grouped)
    }
    
    override init(style: UITableViewStyle) {
        super.init(style: style)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // MARK: - 懒加载模型数组
    lazy var cellData: [JKSettingGroup] = {
        var cellData: [JKSettingGroup] = []
        
        return cellData
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = JKGlobalBackGroundColor
        self.navigationController?.navigationBar.hidden = false
        self.navigationItem.title = "设置"
        
        
        // 要先判断是否是登录状态
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: view.width, height: 44))
        footerView.backgroundColor = UIColor.whiteColor()
        
        let button = UIButton(type: UIButtonType.Custom)
        button.frame = footerView.bounds
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        footerView.addSubview(button)
        
        tableView.tableFooterView = footerView
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: ID)
    }
    
    //MARK: - 登录
    func login(){
        JKLoginTool.show(self)
    }
}

// MARK: - Table view data source and delegate
extension JKBaseSettingTableViewController {
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return cellData.count
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellData[section].items!.count
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let item = cellData[indexPath.section].items![indexPath.row]
        
        let cell = tableView.dequeueReusableCellWithIdentifier(ID)
        cell?.selectionStyle = UITableViewCellSelectionStyle.None
        cell?.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        
        cell?.textLabel?.text = item.title
        
        return cell!
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let item = cellData[indexPath.section].items![indexPath.row]
        
        if let _ = item.operation{
            item.operation!()
        }
        
        if item.vcClass != nil {
            let vc = item.vcClass.dynamicType.init()
            
            self.navigationController?.pushViewController(vc!, animated: true)
        }
    }
}